package com.ftg.learn.util.page;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  分页相关语句查询功能实现
 * @author kangjx
 * @version 1.0
 * @date 2023/5/9 下午3:56
 */
public class SqlDeep<VO> {

    /**
     * 用来生成通用分页的sql
     *
     * @param sql
     * @return
     */
    public String initPageSql(String sql, IPage<VO> ipage) {
        long start = ipage.getCurrent() * ipage.getSize() - ipage.getSize();
        long end = ipage.getCurrent() * ipage.getSize();
        return "select *  from (" + sql + ") t limit " + start + "," + end;
    }

    /**
     * 统计当前sql里面有哪些数据
     * @param sql   语句
     * @return
     */
    public String countPageSql(String sql) {
        return "select count(*) num  from (" + sql + ") t";
    }


    /**
     * 用来生成通用分页的sql
     *
     * @param sql
     * @return
     */
    public String initPageSql1(String sql, QueryWrapper<VO> ew, IPage<VO> ipage) {
        long start = ipage.getCurrent() * ipage.getSize() - ipage.getSize();
        long end = ipage.getCurrent() * ipage.getSize();
        return "select *  from (" + sql + " ${ew.customSqlSegment} ) t limit " + start + "," + end;
    }

    /**
     * 统计当前sql里面有哪些数据
     * @param sql   语句
     * @return
     */
    public String countPageSql1(String sql,QueryWrapper<VO> ew) {
        return "select count(*) num  from (" + sql + " ${ew.customSqlSegment} ) t";
    }

}
